package GUI;

import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import logic.Book;
import logic.Message;

import javax.swing.JTextPane;

import client.OBL_Client;

import java.awt.ScrollPane;
import javax.swing.JCheckBox;

import server.PDFViewer;

import java.awt.Color;
import java.util.ArrayList;
import javax.swing.UIManager;

public class ViewBook extends JPanel{
	
	private Client_GUI parent;
	private JLabel bookTitle;
	private JLabel LBLCategory;
	private JLabel editionLBL;
	private JLabel printDateLBL;
	private JLabel purchaseDataLBL;
	private JLabel locationLBL;
	private JLabel categoryLBL;
	private JLabel author1LBL;
	private JLabel author2LBL;
	private JLabel author3LBL;
	private JLabel author4LBL;
	private JTextPane descriptionText;
	private Book book;
	
	private ResultDisplay results;
	private JLabel lblHotBook;
	private JCheckBox chckbxHotBook;
	private JLabel lblCopies;
	private JLabel lblAvailableCopies;
	private JLabel availableLBL;
	private JLabel copiesLBL;
	private JTextPane catText;
	private JLabel lblNextReturnedCopy;
	private JLabel nextDate;
	private JButton btnIndex;
	private String indexPath;
	private JLabel lblId;
	private JLabel idLBL;
	private JLabel authArray[];
	
	public ViewBook(final Client_GUI parent,final Book book,final ResultDisplay results) {
		setForeground(Color.RED);
		this.parent = parent;
		this.setSize(Client_GUI.MAIN_WIDTH, Client_GUI.MAIN_HEIGHT);
		setLayout(null);
		this.setBackground(UIManager.getColor("inactiveCaption"));

		this.book = book;
		this.results = results;
		
		
		bookTitle = new JLabel("Search Book");
		bookTitle.setForeground(Color.RED);
		bookTitle.setFont(new Font("Tahoma", Font.BOLD, 18));
		bookTitle.setBounds(257, 27, 295, 28);
		add(bookTitle);
		
		JLabel lblEdition = new JLabel("Edition:");
		lblEdition.setFont(new Font("Dialog", Font.BOLD, 13));
		lblEdition.setBounds(76, 71, 113, 14);
		add(lblEdition);
		
		JLabel lblDateOfPurchase = new JLabel("Date of purchase:");
		lblDateOfPurchase.setFont(new Font("Dialog", Font.BOLD, 13));
		lblDateOfPurchase.setBounds(76, 122, 131, 14);
		add(lblDateOfPurchase);
		
		JLabel lblDateOfPrinting = new JLabel("Date of printing:");
		lblDateOfPrinting.setFont(new Font("Dialog", Font.BOLD, 13));
		lblDateOfPrinting.setBounds(76, 97, 131, 14);
		add(lblDateOfPrinting);
		
		JLabel lblPlaceInLibrary = new JLabel("Place in Library:");
		lblPlaceInLibrary.setFont(new Font("Dialog", Font.BOLD, 13));
		lblPlaceInLibrary.setBounds(76, 147, 131, 14);
		add(lblPlaceInLibrary);
		
		JLabel lblAuthors = new JLabel("Authors:");
		lblAuthors.setFont(new Font("Dialog", Font.BOLD, 13));
		lblAuthors.setBounds(400, 66, 70, 14);
		add(lblAuthors);
		
		JLabel lblCategory = new JLabel("Category:");
		lblCategory.setFont(new Font("Dialog", Font.BOLD, 13));
		lblCategory.setBounds(76, 172, 131, 14);
		add(lblCategory);
		
		btnIndex = new JButton("View Index");
		btnIndex.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnIndex.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				//PDFViewer pdf = new PDFViewer(indexPath);
				
				ArrayList<String> index = new ArrayList<String>();
				ArrayList<Object> data = new ArrayList<Object>();
				
				int action = Message.PDF_REQUEST;
				
				index.add("BID");
				data.add(book.getBID());
				
				Message toSend =  new Message(index, data, action);
				
				parent.sendMessageToServer(toSend);
				
			}
		});
		btnIndex.setVisible(false);
		btnIndex.setBounds(209, 382, 148, 51);
		add(btnIndex);
		
		JButton btnCancel = new JButton("Back");
		btnCancel.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				parent.RepaintMain(results);
								
			}
		});
		btnCancel.setBounds(482, 382, 148, 51);
		add(btnCancel);
		
		editionLBL = new JLabel("");
		editionLBL.setFont(new Font("Dialog", Font.BOLD, 13));
		editionLBL.setBounds(199, 71, 136, 14);
		add(editionLBL);
		
		printDateLBL = new JLabel("");
		printDateLBL.setFont(new Font("Dialog", Font.BOLD, 13));
		printDateLBL.setBounds(199, 97, 136, 14);
		add(printDateLBL);
		
		purchaseDataLBL = new JLabel("");
		purchaseDataLBL.setFont(new Font("Dialog", Font.BOLD, 13));
		purchaseDataLBL.setBounds(199, 122, 136, 14);
		add(purchaseDataLBL);
		
		locationLBL = new JLabel("");
		locationLBL.setFont(new Font("Dialog", Font.BOLD, 13));
		locationLBL.setBounds(199, 147, 136, 14);
		add(locationLBL);
		
		categoryLBL = new JLabel("");
		categoryLBL.setFont(new Font("Dialog", Font.BOLD, 13));
		categoryLBL.setBounds(199, 172, 136, 14);
		add(categoryLBL);
		
		author1LBL = new JLabel("");
		author1LBL.setFont(new Font("Dialog", Font.BOLD, 13));
		author1LBL.setBounds(480, 66, 124, 14);
		add(author1LBL);
		
		author2LBL = new JLabel("");
		author2LBL.setFont(new Font("Dialog", Font.BOLD, 13));
		author2LBL.setBounds(480, 90, 124, 14);
		add(author2LBL);
		
		author3LBL = new JLabel("");
		author3LBL.setFont(new Font("Dialog", Font.BOLD, 13));
		author3LBL.setBounds(480, 113, 124, 14);
		add(author3LBL);
		
		author4LBL = new JLabel("");
		author4LBL.setFont(new Font("Dialog", Font.BOLD, 13));
		author4LBL.setBounds(480, 135, 124, 14);
		add(author4LBL);
		
		authArray = new JLabel[4];
		authArray[0] = author1LBL;
		authArray[1] = author2LBL;
		authArray[2] = author3LBL;
		authArray[3] = author4LBL;
		
		ScrollPane scrollPane = new ScrollPane();
		scrollPane.setBounds(10, 230, 620, 84);
		add(scrollPane);
		
		descriptionText = new JTextPane();
		descriptionText.setBackground(UIManager.getColor("control"));
		descriptionText.setEditable(false);
		descriptionText.setBounds(10, 278, 609, 60);
		scrollPane.add(descriptionText);
		
		lblHotBook = new JLabel("Hot Book:");
		lblHotBook.setFont(new Font("Dialog", Font.BOLD, 13));
		lblHotBook.setBounds(400, 167, 131, 14);
		add(lblHotBook);
		
		chckbxHotBook = new JCheckBox("Hot Book");
		chckbxHotBook.setFont(new Font("Dialog", Font.BOLD, 13));
		chckbxHotBook.setEnabled(false);
		chckbxHotBook.setBounds(480, 163, 97, 23);
		add(chckbxHotBook);
		
		lblCopies = new JLabel("Copies:");
		lblCopies.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblCopies.setBounds(21, 202, 131, 14);
		add(lblCopies);
		
		lblAvailableCopies = new JLabel("Available copies:");
		lblAvailableCopies.setForeground(Color.BLACK);
		lblAvailableCopies.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblAvailableCopies.setBounds(220, 202, 131, 14);
		add(lblAvailableCopies);
		
		availableLBL = new JLabel((String) null);
		availableLBL.setForeground(Color.RED);
		availableLBL.setBounds(330, 203, 88, 14);
		add(availableLBL);
		
		copiesLBL = new JLabel((String) null);
		copiesLBL.setBounds(144, 203, 88, 14);
		add(copiesLBL);
		
		JPanel panel = new JPanel();
		panel.setBounds(651, 97, 135, 221);
		add(panel);
		panel.setLayout(null);
		
		catText = new JTextPane();
		catText.setBackground(UIManager.getColor("control"));
		catText.setEditable(false);
		catText.setBounds(0, 0, 135, 221);
		panel.add(catText);
		
		JLabel lblCategories = new JLabel("Categories:");
		lblCategories.setFont(new Font("Dialog", Font.BOLD, 13));
		lblCategories.setBounds(655, 71, 131, 15);
		add(lblCategories);
		
		lblNextReturnedCopy = new JLabel("Next Returned Copy:");
		lblNextReturnedCopy.setForeground(Color.BLACK);
		lblNextReturnedCopy.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblNextReturnedCopy.setBounds(400, 202, 140, 14);
		add(lblNextReturnedCopy);
		
		nextDate = new JLabel((String) null);
		nextDate.setForeground(Color.RED);
		nextDate.setFont(new Font("Dialog", Font.BOLD, 13));
		nextDate.setBounds(532, 202, 113, 14);
		add(nextDate);
		
		lblId = new JLabel("ID:");
		lblId.setFont(new Font("Dialog", Font.BOLD, 13));
		lblId.setBounds(76, 46, 113, 14);
		add(lblId);
		
		idLBL = new JLabel("0");
		idLBL.setFont(new Font("Dialog", Font.BOLD, 13));
		idLBL.setBounds(199, 41, 23, 14);
		add(idLBL);
		
		nextDate.setVisible(false);
		lblNextReturnedCopy.setVisible(false);
		InitBookData();
		
	}
	
	private void InitBookData(){
		bookTitle.setText(book.getname());
		editionLBL.setText(String.valueOf(book.getedition()));
		printDateLBL.setText(book.getprintDate());
		purchaseDataLBL.setText(book.getpurchaseDate());
		locationLBL.setText(book.getlocation());
		copiesLBL.setText(String.valueOf(book.getcopies()));
		availableLBL.setText(String.valueOf(book.getavailableCopies()));
		if (book.gethotBook() == 1) chckbxHotBook.setSelected(true);
		idLBL.setText(String.valueOf(book.getBID()));
				
		if (Integer.valueOf(book.getbookIndex()) == 1) {
			
			indexPath = "INDEXES/" + book.getBID() + ".pdf"; 
			btnIndex.setVisible(true);
		}else indexPath = null;
		
		ArrayList<String> categories = book.getcategories();
		for(String categorie : categories){
			catText.setText(catText.getText() + categorie + "\n");
		}
		
		if(book.getavailableCopies() == 0){
			nextDate.setText(book.getnextBook());
			nextDate.setVisible(true);
			lblNextReturnedCopy.setVisible(true);
		}
				
		ArrayList<String> authorList = book.getAuthors();
		for(int i=0; i<authorList.size(); i++)
			authArray[i].setText(authorList.get(i));
		
		descriptionText.setText(book.getdescription());
		
	}
}
